﻿using BXS.API.Models;
using Microsoft.AspNetCore.Mvc;

namespace BXS.API.Controllers
{
    /// <summary>
    /// 基础控制器
    /// </summary>
    [ApiController]
    [Route("api/v1/[controller]")]
    public abstract class BaseController : ControllerBase
    {
        /// <summary>
        /// 返回成功响应
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns>成功响应</returns>
        protected ActionResult<ApiResponse<T>> Success<T>(T data, string message = "成功")
        {
            return Ok(ApiResponse<T>.Success(data, message));
        }

        /// <summary>
        /// 返回错误响应
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns>错误响应</returns>
        protected ActionResult<ApiResponse<T>> Error<T>(string message, int code = 500)
        {
            return StatusCode(code, ApiResponse<T>.Error(message, code));
        }

        /// <summary>
        /// 返回分页响应
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">数据列表</param>
        /// <param name="total">总记录数</param>
        /// <param name="page">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="message">消息</param>
        /// <returns>分页响应</returns>
        protected ActionResult<PagedApiResponse<T>> PagedSuccess<T>(List<T> data, int total, int page, int pageSize, string message = "成功")
        {
            return Ok(PagedApiResponse<T>.Success(data, total, page, pageSize, message));
        }
    }
}